﻿@using Microsoft.AspNetCore.Components.Rendering
@using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure
@namespace Microsoft.FluentUI.AspNetCore.Components
@typeparam TGridItem
@{
    Grid.AddColumn(this, InitialSortDirection, IsDefaultSortColumn);
}
@code
{
    private void RenderDefaultHeaderContent(RenderTreeBuilder __builder)
    {
        @if (HeaderCellItemTemplate is not null)
        {
            @HeaderCellItemTemplate(this)
        }
        else if (Grid.HeaderCellAsButtonWithMenu)
        {
            string? tooltip = Tooltip ? (HeaderTooltip ?? Title) : null;

            <FluentKeyCode Only="new [] { KeyCode.Ctrl, KeyCode.Enter}" OnKeyDown="HandleKeyDown" class="keycapture">
                <span class="col-sort-container" @oncontextmenu="@(() => Grid.RemoveSortByColumnAsync(this))" @oncontextmenu:preventDefault>
                    <FluentButton Disabled="@(!AnyColumnActionEnabled)" Id="@_columnId" Appearance="Appearance.Stealth" class="col-sort-button" @onclick="@HandleColumnHeaderClickedAsync" aria-label="@tooltip" title="@tooltip">
                        <div class="col-title-text" title="@tooltip">@Title</div>

                        @if (Grid.SortByAscending.HasValue && ShowSortIcon)
                        {
                            if (Grid.SortByAscending == true)
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.ArrowSortUp())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                            }
                            else
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.ArrowSortDown())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                            }
                        }
                        @if (Grid.ResizeType is not null && ColumnOptions is not null)
                        {
                            @if (Filtered.GetValueOrDefault())
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                            }
                        }
                    </FluentButton>
                    <FluentMenu Anchor="@_columnId" @bind-Open="@_isMenuOpen" HorizontalViewportLock="false" HorizontalPosition="HorizontalPosition.End">
                        @if (Sortable.HasValue ? Sortable.Value : IsSortableByDefault())
                        {
                            <FluentMenuItem OnClick="@(async () => await Grid.SortByColumnAsync(this))" @onkeydown="HandleSortMenuKeyDownAsync">
                                @GetSortOptionText()
                            </FluentMenuItem>
                        }
                        @if (Grid.ResizeType is not null && Grid.ResizableColumns)
                        {
                            <FluentMenuItem OnClick="@(async () => await Grid.ShowColumnResizeAsync(this))" @onkeydown="HandleResizeMenuKeyDownAsync">@Grid.ColumnResizeLabels.ResizeMenu</FluentMenuItem>
                        }
                        @if (ColumnOptions is not null)
                        {
                            <FluentMenuItem OnClick="@(async () => await Grid.ShowColumnOptionsAsync(this))" @onkeydown="HandleOptionsMenuKeyDownAsync">@Grid.ColumnOptionsLabels.OptionsMenu</FluentMenuItem>
                        }
                    </FluentMenu>
                </span>
            </FluentKeyCode>
        }
        else
        {
            string? tooltip = Tooltip ? (HeaderTooltip ?? Title) : null;

            @if (Align == Align.Start || Align == Align.Center)
            {
                @if (Grid.ResizeType is not null)
                {
                    <FluentButton Appearance="Appearance.Stealth" class="col-options-button" @onclick="@(() => Grid.ShowColumnOptionsAsync(this))" aria-label="Resize/filter this column">
                        <FluentIcon Value="@(new CoreIcons.Regular.Size24.ChevronDown())" Color="Color.Neutral" Width="20px" Style="opacity: 0.5;" />
                    </FluentButton>
                }
                else
                {
                    @if (ColumnOptions is not null)
                    {
                        <FluentButton Appearance="Appearance.Stealth" class="col-options-button" @onclick="@(() => Grid.ShowColumnOptionsAsync(this))" aria-label="Filter this column">
                            @if (Filtered.GetValueOrDefault())
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.FilterDismiss())" />
                            }
                            else
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" />
                            }

                        </FluentButton>
                    }
                }
            }

            if (Sortable.HasValue ? Sortable.Value : IsSortableByDefault())
            {
                <FluentKeyCode Only="new [] { KeyCode.Ctrl, KeyCode.Enter}" OnKeyDown="HandleKeyDown" class="keycapture">
                    <span class="col-sort-container" @oncontextmenu="@(() => Grid.RemoveSortByColumnAsync(this))" @oncontextmenu:preventDefault>
                        <FluentButton Appearance="Appearance.Stealth" class="col-sort-button" @onclick="@(() => Grid.SortByColumnAsync(this))" aria-label="@tooltip" title="@tooltip">
                            <div class="col-title-text" title="@tooltip">@Title</div>

                            @if (Grid.SortByAscending.HasValue && ShowSortIcon)
                            {
                                if (Grid.SortByAscending == true)
                                {
                                    <FluentIcon Value="@(new CoreIcons.Regular.Size24.ArrowSortUp())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                                }
                                else
                                {
                                    <FluentIcon Value="@(new CoreIcons.Regular.Size24.ArrowSortDown())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                                }
                            }
                            @if (Grid.ResizeType is not null && ColumnOptions is not null)
                            {
                                @if (Filtered.GetValueOrDefault())
                                {
                                    <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                                }
                            }
                        </FluentButton>
                    </span>
                </FluentKeyCode>
            }
            else
            {
                <div class="col-title">
                    <div class="col-title-text" title="@tooltip">@Title</div>
                    @if (Grid.ResizeType is not null && ColumnOptions is not null)
                    {
                        @if (Filtered.GetValueOrDefault())
                        {
                            <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" Slot="@(Align == Align.End ? "start" : "end")" Style="opacity: 0.5;" />
                        }
                    }
                </div>
            }

            @if (Align == Align.End)
            {
                @if (Grid.ResizeType is not null)
                {
                    <FluentButton Appearance="Appearance.Stealth" class="col-options-button" @onclick="@(() => Grid.ShowColumnOptionsAsync(this))" aria-label="Filter this column">
                        <FluentIcon Value="@(new CoreIcons.Regular.Size24.ChevronDown())" Color="Color.Neutral" Width="20px" Style="opacity: 0.5;" />
                    </FluentButton>
                }
                else
                {
                    @if (ColumnOptions is not null)
                    {
                        <FluentButton Appearance="Appearance.Stealth" class="col-options-button" @onclick="@(() => Grid.ShowColumnOptionsAsync(this))" aria-label="Filter this column">
                            @if (Filtered.GetValueOrDefault())
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.FilterDismiss())" />
                            }
                            else
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" />
                            }

                        </FluentButton>
                    }
                }
            }
        }
    }

    internal void RenderPlaceholderContent(RenderTreeBuilder __builder, PlaceholderContext placeholderContext)
    {
        // Blank if no placeholder template was supplied, as it's enough to style with CSS by default
        if (PlaceholderTemplate is not null)
        {
            @PlaceholderTemplate(placeholderContext)
        }
    }
}
